﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace AncientEmpires_025_132
{
    public class Cell : VisibleGameEntity
    {
        private ButtonState PreButtonState;
        // Cho biết cell có thể đi lên được không.
        private bool _isMoveable;
        public bool IsMoveable
        {
            get { return _isMoveable; }
            set { _isMoveable = value; }
        }
        // Kiểm tra cell đã có nhân vật hoặc là building đang đứng hay không.
        private int _isUsed;
        public int IsUsed
        {
            get { return _isUsed; }
            set { _isUsed = value; }
        }

        private Vector2 _indexInMaTrix;
        public Vector2 IndexInMaTrix
        {
            get { return _indexInMaTrix; }
            set { _indexInMaTrix = value; }
        }

        // Sự kiện khi click chuột vào cell.
        public delegate void EventHandleSelected(Vector2 indexMatrix);
        public event EventHandleSelected EventSelected;
        public Cell(ContentManager content, string strtexture, Vector2 pos, Vector2 size, Vector2 topleft, Vector2 indexmatrix)
        {

            Texture2D[] texture = new Texture2D[1];
            texture[0] = content.Load<Texture2D>(strtexture);
            ListSprites = new List<Sprite2D>();
            Sprite2D tempSprite = new Sprite2D(texture, pos);
            tempSprite.TopLeft = topleft;
            tempSprite.Size = size;
            IndexInMaTrix = indexmatrix;
            ListSprites.Add(tempSprite);
            PreButtonState = ButtonState.Released;
            IsUsed = IndexNation.Available;
        }

        public override void Update(GameTime gametime)
        {
            //for (int i = 0; i < ListSprites.Count; i++)
            //{
            //    ListSprites[i].Position = -ListSprites[i].TopLeft;
            //}
            MouseState ms = Mouse.GetState();
            if (ms.LeftButton == ButtonState.Pressed && PreButtonState == ButtonState.Released)
            {
                PreButtonState = ButtonState.Pressed;
                if (CheckClickSprite(new Vector2(ms.X, ms.Y)))
                {
                    EventSelected(IndexInMaTrix);
                }
            }
            else
            {
                PreButtonState = ButtonState.Released;
                if (ms.LeftButton == ButtonState.Released)
                {
                    if(CheckHoverSpirte(new Vector2(ms.X, ms.Y)))
                    {
                        IsHover = true;
                    }
                    else IsHover = false;
                }
            }
            base.Update(gametime);
        }

        public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            //for (int i = 0; i < ListSprites.Count; i++)
            //{
            //    Rectangle rect = new Rectangle();
            //    if (ListSprites[i].Size.X == 0 && ListSprites[i].Size.Y == 0)
            //    {
            //        ListSprites[i].Size = new Vector2(ListSprites[i].Textures[ListSprites[i].ITexture].Width, ListSprites[i].Textures[ListSprites[i].ITexture].Height);
            //    }
            //    rect = new Rectangle((int)ListSprites[i].Position.X, (int)ListSprites[i].Position.Y, (int)ListSprites[i].Size.X, (int)ListSprites[i].Size.Y);
            //    spriteBatch.Draw(ListSprites[i].Textures[ListSprites[i].ITexture], rect, Color.White);
            //}
            base.Draw(gameTime, spriteBatch);
        }
    }
}
